package cn.edu.sdjzu.xg.bysj.service;

import cn.edu.sdjzu.xg.bysj.dao.GraduateProjectCategoryDao;
import cn.edu.sdjzu.xg.bysj.domain.GraduateProjectCategory;
import util.JdbcHelper;
import util.Pagination;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

public final class GraduateProjectCategoryService {
    private GraduateProjectCategoryDao graduateProjectCategoryDao =
            GraduateProjectCategoryDao.getInstance();
    //本类的一个对象引用，保存自身对象
    private static GraduateProjectCategoryService graduateProjectCategoryService =
            new GraduateProjectCategoryService();

    //私有的构造方法，防止其它类创建它的对象
    private GraduateProjectCategoryService() {
    }

    //静态方法，返回本类的惟一对象
    public synchronized static GraduateProjectCategoryService getInstance() {
        return graduateProjectCategoryService;
    }

    public Collection<GraduateProjectCategory> findAll()
            throws SQLException {
        Connection connection = JdbcHelper.getConn();
        Collection<GraduateProjectCategory> graduateProjectCategories =
                graduateProjectCategoryDao.findAll(connection);
        JdbcHelper.close(connection);
        return graduateProjectCategories;
    }

    public GraduateProjectCategory find(Integer id) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        GraduateProjectCategory graduateProjectCategory =
                graduateProjectCategoryDao.find(id, connection);
        JdbcHelper.close(connection);
        return graduateProjectCategory;
    }

    public boolean update(GraduateProjectCategory graduateProjectCategory) throws SQLException {
        //获取数据库连接
        Connection connection = JdbcHelper.getConn();
        boolean updated = false;
        //将自动提交设置为false
        connection.setAutoCommit(false);
        try {
            updated = graduateProjectCategoryDao.update(graduateProjectCategory, connection);
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            //回滚
            connection.rollback();
        } finally {
            //将自动提交设置为true
            connection.setAutoCommit(true);
            //释放连接
            JdbcHelper.close(connection);
        }
        return updated;
    }

    public boolean add(GraduateProjectCategory graduateProjectCategory) throws SQLException {
        //获取数据库连接
        Connection connection = JdbcHelper.getConn();
        boolean added = false;
        //将自动提交设置为false
        connection.setAutoCommit(false);
        try {
            added = graduateProjectCategoryDao.add(graduateProjectCategory, connection);
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            //回滚
            connection.rollback();
        } finally {
            //将自动提交设置为true
            connection.setAutoCommit(true);
            //释放连接
            JdbcHelper.close(connection);
        }
        return added;
    }

    public boolean delete(Integer id) throws SQLException {
        //获取数据库连接
        Connection connection = JdbcHelper.getConn();
        boolean deleted = false;
        //将自动提交设置为false
        connection.setAutoCommit(false);
        try {
            deleted = graduateProjectCategoryDao.delete(id, connection);
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            //回滚
            connection.rollback();
        } finally {
            //将自动提交设置为ture
            connection.setAutoCommit(true);
            //释放连接
            JdbcHelper.close(connection);
        }
        return deleted;
    }

    public boolean delete(GraduateProjectCategory graduateProjectCategory) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        boolean deleted = graduateProjectCategoryDao.delete(graduateProjectCategory, connection);
        JdbcHelper.close(connection);
        return deleted;
    }
}
